
package com.kweative.model.query;


import com.kweative.model.Table;
import com.kweative.model.column.Column;
import org.apache.commons.lang.StringEscapeUtils;

/**
 *
 * @author henrik
 */
public class QueryUtil {
    public static String formatTable(Table table) {
        if (table.getAlias() != null)
            return String.format("`%s`.`%s` AS `%s`",table.getSchema().getName(),table.getName(),table.getAlias());
        else
            return String.format("`%s`.`%s`",table.getSchema().getName(),table.getName());
    }
    
    public static String formatColumn(Column col) {
        String tblName = col.getTable().getAlias() != null ? col.getTable().getAlias() : col.getTable().getName();
        return String.format("`%s`.`%s`",tblName,col.getName());
    }
    public static String formatColumn(Column col,boolean useAlias) {
        String tblName = col.getTable().getAlias() != null ? col.getTable().getAlias() : col.getTable().getName();

        if (useAlias && col.getAlias() != null && col.getAlias().length() > 0)
            return String.format("`%s`.`%s` AS `%s`",tblName,col.getName(),col.getAlias());

        return formatColumn(col);
    }
    public static String formatValue(Column col,Object value) {
        if (value instanceof Column)
            return formatColumn((Column)value);
        else {
            return col.serialize(value);
        }
    }
    public static String[] formatValue(Column col,Object[] arr) {
        String result[] = new String[arr.length];

        for(int i = 0;i < arr.length; i++) {
            result[i] = formatValue(col,arr[i]);
        }
        return result;
    }
}
